//
//  TPIViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/6.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "TPIViewController.h"
#import "FindMoreTableViewCell.h"
#import "MusicListTableViewCell.h"
#import "MusicListViewController.h"
#import "Player.h"

@interface TPIViewController ()<UITableViewDataSource,UITableViewDelegate>
// 作者信息
@property(nonatomic,strong)NSMutableDictionary *ownerInfoDictionary;
// 专辑信息
@property(nonatomic,strong)NSMutableDictionary *specialInfoDictionary;
// 声音信息
@property(nonatomic,strong)NSMutableDictionary *soundInfoDictionary;
// 作者头像的背景图
@property(nonatomic,strong)UIImageView *ownerBgImageView;
@property(nonatomic,strong)UIVisualEffectView *effectView;
@property(nonatomic,assign)CGFloat lastScrollOffset;

// 判断向上还是向下
@property(nonatomic,assign)BOOL isUp;
// say
@property(nonatomic,strong)UILabel *sayLabel;
// say height
@property(nonatomic,assign)CGFloat sayHeight;

@property(nonatomic,strong)UITableView *proInfoTableView;
@end

@implementation TPIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initProInfoTableView];
    // 作者信息
    [self getOwnerInfoWithUrl];
    // 获取专辑信息
    [self getSpecialInfoWithUrl];
    // 获取声音信息
    [self getSoundInfoWithUrl];
}

/**
 *  初始化broTableView
 */
- (void)initProInfoTableView
{
    // 初始化作者头像
    self.ownerBgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*300)];
    self.ownerBgImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    self.ownerBgImageView.userInteractionEnabled = YES;
    //  创建需要的毛玻璃特效类型
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    //  毛玻璃view 视图
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    self.effectView.frame = CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*300);
    //  设置模糊透明度
    self.effectView.alpha = 0.9;
    [self.ownerBgImageView addSubview:_effectView];
    [self.view addSubview:_ownerBgImageView];
    
    // 初始化tableview
    self.proInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, FM_Screen_Width,FM_Screen_Height-48) style:UITableViewStyleGrouped];
    self.proInfoTableView.backgroundColor = [UIColor clearColor];
    self.proInfoTableView.dataSource = self;
    self.proInfoTableView.delegate = self;
    [self.view addSubview:_proInfoTableView];
    
    // 专辑
    [self.proInfoTableView registerClass:[FindMoreTableViewCell class] forCellReuseIdentifier:@"zhuanjiCell"];
    // 声音
    [self.proInfoTableView registerClass:[MusicListTableViewCell class] forCellReuseIdentifier:@"shengyinCell"];
}

// UITableViewDataSource 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 头
    if (section == 0) {
        return 0;
    }
    // 专辑
    else if(section == 1)
    {
        return [[self.specialInfoDictionary objectForKey:@"list"] count];
    }
    // 声音
    else
    {
        return [[self.soundInfoDictionary objectForKey:@"list"] count];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // 头
    if (section == 0) {
        return FM_Screen_Pts_Height*300;
    }
    // 专辑
    else if(section == 1)
    {
        if (self.specialInfoDictionary.count != 0) {
            return FM_Screen_Pts_Height*50;
        }else{
            return 0.000001f;
        }
    }
    // 声音
    else
    {
        if (self.soundInfoDictionary.count != 0 && section == 2) {
            return FM_Screen_Pts_Height*50;
        }else{
            return 0.000001f;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 头
    if (section == 0) {
      [self.ownerBgImageView sd_setImageWithURL:[NSURL URLWithString:[self.ownerInfoDictionary objectForKey:@"backgroundLogo"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*300)];
        
        // 头像
        UIImageView *ownerImageView = [[UIImageView alloc] init];
        ownerImageView.frame = CGRectMake(FM_Screen_Width/2-FM_Screen_Pts_Width*50, FM_Screen_Pts_Height*70, FM_Screen_Pts_Width*130, FM_Screen_Pts_Width*130);
        ownerImageView.layer.cornerRadius = FM_Screen_Pts_Width*130/2;
        ownerImageView.layer.masksToBounds = YES;
        [ownerImageView sd_setImageWithURL:[NSURL URLWithString:[self.ownerInfoDictionary objectForKey:@"backgroundLogo"]] placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
        [view addSubview:ownerImageView];
        
        // 昵称
        UILabel *nickNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*50, FM_Screen_Pts_Height*70+FM_Screen_Pts_Width*135, FM_Screen_Width-FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*20)];
        nickNameLabel.font = [UIFont boldSystemFontOfSize:17];
        nickNameLabel.textAlignment = NSTextAlignmentCenter;
        nickNameLabel.textColor = [UIColor whiteColor];
        nickNameLabel.text = [self.ownerInfoDictionary objectForKey:@"nickname"];
        [view addSubview:nickNameLabel];
        
        // say
        self.sayLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*50, FM_Screen_Pts_Height*70+FM_Screen_Pts_Width*165, FM_Screen_Width-FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*20)];
        self.sayLabel.text = [self.ownerInfoDictionary objectForKey:@"personalSignature"];
        self.sayLabel.font = [UIFont systemFontOfSize:13];
        self.sayLabel.textAlignment = NSTextAlignmentCenter;
        self.sayLabel.textColor = [UIColor grayColor];
        [view addSubview:self.sayLabel];
        return view;
    }
    // 专辑
    else if(section == 1)
    {
        if (self.specialInfoDictionary.count != 0) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*50)];
            view.backgroundColor = UIColorFromRGB(0xe7e7e7);
            UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*20, 0, view.frame.size.width-FM_Screen_Pts_Width*20, view.frame.size.height)];
            countLabel.textColor = [UIColor grayColor];
            countLabel.text = [NSString stringWithFormat:@"发表的专辑 (%ld)",[[self.specialInfoDictionary objectForKey:@"list"] count]];
            [view addSubview:countLabel];
            return view;
        }else{
            UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
            return view;
        }
    }
    // 声音
    else
    {
        if (self.soundInfoDictionary.count != 0) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*50)];
            view.backgroundColor = UIColorFromRGB(0xe7e7e7);
            UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*20, 0, view.frame.size.width-FM_Screen_Pts_Width*20, view.frame.size.height)];
            countLabel.textColor = [UIColor grayColor];
            countLabel.text = [NSString stringWithFormat:@"发表的声音 (%ld)",[[self.soundInfoDictionary objectForKey:@"list"] count]];
            [view addSubview:countLabel];
            return view;
        }else{
            UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
            return view;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 头
    if (indexPath.section == 0) {
        return FM_Screen_Pts_Height*300;
    }
    // 专辑
    else if(indexPath.section == 1)
    {
        if (self.specialInfoDictionary.count != 0) {
            return FM_Screen_Pts_Width*70 + FM_Screen_Pts_Height*40;
        }else{
            return 0;
        }
    }
    // 声音
    else
    {
        if (self.soundInfoDictionary.count != 0) {
            return FM_Screen_Pts_Width*70 + FM_Screen_Pts_Height*20;
        }else{
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *proCell = [tableView dequeueReusableCellWithIdentifier:@"proCell"];
    if (indexPath.section == 0)
    {
    }
    else if(indexPath.section == 1)
    {
        // 专辑
        FindMoreTableViewCell *zhuanjiCell = [tableView dequeueReusableCellWithIdentifier:@"zhuanjiCell"];
        zhuanjiCell.findMoreDic = [[self.specialInfoDictionary objectForKey:@"list"] objectAtIndex:indexPath.row];
        return zhuanjiCell;
    }
    else
    {
        // 声音
        MusicListTableViewCell *shengyinCell = [tableView dequeueReusableCellWithIdentifier:@"shengyinCell"];
        shengyinCell.bodyDictionary = [[self.soundInfoDictionary objectForKey:@"list"] objectAtIndex:indexPath.row];
        return shengyinCell;
    }
    return proCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 专辑
    if (indexPath.section == 1) {
        MusicListViewController *musicListVC = [[MusicListViewController alloc] init];
        musicListVC.musicListDic = [[self.specialInfoDictionary objectForKey:@"list"] objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:musicListVC animated:YES];
    }
    
    // 单曲
    if (indexPath.section == 2) {
        Player *player = [Player shareInstance];
        [player collectPreseve];
        self.tabBarController.selectedIndex = 2;
        [self playingFM:indexPath.row requestUrl:nil];
    }
}

// 播放音频
- (void)playingFM:(NSInteger)index requestUrl:(NSString *)requestUrl
{
    Player *play = [Player shareInstance];
    if (play.striDic != nil) {
        [play collectPreseve];
    }
    play.striDic = self.soundInfoDictionary;
    play.num = index;
    play.webSite = requestUrl;
    play.listArr = [self.soundInfoDictionary objectForKey:@"list"];
    [play playFromNet];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 获取作者信息
- (void)getOwnerInfoWithUrl
{
    self.ownerInfoDictionary = [NSMutableDictionary dictionary];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?device=iPhone&toUid=%@",[[self.tpiDic objectForKey:@"uid"] stringValue]];
    [NetWorkManager GET:url isCaChe:NO resultData:^(id responseObject) {
        self.ownerInfoDictionary = responseObject;
        [self.proInfoTableView reloadData];
    } resultRrror:^(NSError *error) {
        NSLog(@"主播信息:error,%@",error);
    }];
}
// 获取专辑信息
- (void)getSpecialInfoWithUrl
{
    self.specialInfoDictionary = [NSMutableDictionary dictionary];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/1/2?device=iPhone",[[self.tpiDic objectForKey:@"uid"] stringValue]];
    [NetWorkManager GET:url isCaChe:NO resultData:^(id responseObject) {
        self.specialInfoDictionary = responseObject;
        [self.proInfoTableView reloadData];
    } resultRrror:^(NSError *error) {
        NSLog(@"主播专辑信息:error,%@",error);
    }];
}
// 获取声音信息
- (void)getSoundInfoWithUrl
{
    self.soundInfoDictionary = [NSMutableDictionary dictionary];
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30?device=iPhone",[[self.tpiDic objectForKey:@"uid"] stringValue]];
    [NetWorkManager GET:url isCaChe:NO resultData:^(id responseObject) {
        self.soundInfoDictionary = responseObject;
        [self.proInfoTableView reloadData];
    } resultRrror:^(NSError *error) {
        NSLog(@"主播声音信息:error,%@",error);
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // 加线
    self.navigationController.navigationBar.layer.masksToBounds = NO;
    // 导航栏非透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsCompact];
}

- (void)viewWillAppear:(BOOL)animated
{
    // 去线
    self.navigationController.navigationBar.layer.masksToBounds = YES;
    // 导航栏透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bigShadow.png"] forBarMetrics:UIBarMetricsCompact];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < 0) {
        // 调整背景图大小
        CGRect bgImageViewFrame = self.ownerBgImageView.frame;
        bgImageViewFrame.size.height =  FM_Screen_Pts_Height*300-y;
        self.ownerBgImageView.frame = bgImageViewFrame;
        
        // 调整背景图的毛玻璃大小
        CGRect bgEffectFrame = self.effectView.frame;
        bgEffectFrame.size.height =  FM_Screen_Pts_Height*300-y;
        self.effectView.frame = bgEffectFrame;
        
        // 调整虚拟度
        if (scrollView.contentOffset.y > self.lastScrollOffset) {
            //用户往上拖动，也就是屏幕内容向下滚动
            if (self.effectView.alpha<0.9) {
                self.effectView.alpha += 0.02;
            }
        } else {
            //用户往下拖动，也就是屏幕内容向上滚动
            if (self.effectView.alpha>0.3)
            {
                self.effectView.alpha -= 0.02;
            }
        }
        self.lastScrollOffset = scrollView.contentOffset.y;
    }
    else if(y == 0.000000){
        self.proInfoTableView.frame = CGRectMake(0,0, FM_Screen_Width,FM_Screen_Height-48);
        self.ownerBgImageView.frame = CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*300);
        self.effectView.frame = CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Height*300);
    }
    else
    {
       // 调整背景图位置
       CGRect bgImageViewFrame = self.ownerBgImageView.frame;
       bgImageViewFrame.origin.y = -y;
       self.ownerBgImageView.frame = bgImageViewFrame;
    }
}

@end
